/*
 * Copyright (C) 2011 Fred Barrie This program is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version. This program is distributed
 * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details. You should have received
 * a copy of the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */
package org.aergia.vinny.gui.util;

import java.awt.event.FocusEvent;
import java.util.Calendar;

import org.aergia.vinny.util.DateUtil;

/**
 * @author fred
 */
@SuppressWarnings("serial")
public class TimeEntry extends TextField {

	private Calendar time;

	public TimeEntry(final Calendar time) {
		this.time = time;
		this.updateTextField();
	}

	@Override
	public void fireActionPerformed() {
		final Calendar result = DateUtil.parseHour(this.time, this.getText());
		if (result != null) {
			this.time = result;
			this.updateTextField();
		}
		super.fireActionPerformed();
	}

	@Override
	public void focusLost(final FocusEvent e) {
		super.focusLost(e);
		this.fireActionPerformed();
	}

	public Calendar getTime() {
		return this.time;
	}

	public void setTime(final Calendar time) {
		this.time = time;
		this.updateTextField();
	}

	@Override
	protected boolean verify() {
		final String text = this.getText();
		final Calendar result = DateUtil.parseHour(this.time, text);
		if (result != null) {
			return true;
		}
		return false;
	}

	private void updateTextField() {
		this.setText(DateUtil.formatHour(this.time));
	}

}
